package web.controller;

import module.model.SysTreeModel;
import module.service.SysTreeService;
import module.service.UserService;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.*;
import org.springframework.security.web.authentication.session.SessionAuthenticationException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import web.vo.Node;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/admin")
public class AdminCore extends BaseController{

    @Autowired
    private SysTreeService sysTreeService;

    /**
     * Spring security 最后登录异常Session名称
     */
    public static final String SPRING_SECURITY_LAST_EXCEPTION = "SPRING_SECURITY_LAST_EXCEPTION";

    @RequestMapping(value = "login")
    public String login(HttpSession httpSession, ModelMap map){
        Exception springSecurityLastException = (Exception) httpSession.getAttribute(SPRING_SECURITY_LAST_EXCEPTION);
        if (springSecurityLastException != null) {
            if (springSecurityLastException instanceof BadCredentialsException) {
                String loginId = ((String) httpSession.getAttribute("SPRING_SECURITY_LAST_USERNAME"));
                //SysUser sysUser = sysUserService.findByAdminLoginId(loginId);
                /*if (sysUser == null) {

                }else{

                }*/
                map.put("error","您填写的用户名或密码错误");
            }else if (springSecurityLastException instanceof DisabledException) {
                map.put("error", "您的账号已被禁用,无法登录!");
            } else if (springSecurityLastException instanceof LockedException) {
                map.put("error", "您的账号已被锁定,无法登录!");
            } else if (springSecurityLastException instanceof AccountExpiredException) {
                map.put("error", "您的账号已过期,无法登录!");
            } else if (springSecurityLastException instanceof AuthenticationServiceException){
                map.put("error", springSecurityLastException.getMessage());
            } else if (springSecurityLastException instanceof SessionAuthenticationException){
                map.put("error", "该用户已经在别处登录,无法登录!");
            } else {
                map.put("error", "出现未知错误,无法登录!");
            }
            httpSession.removeAttribute(SPRING_SECURITY_LAST_EXCEPTION);
        }
        return "admin/login";
    }

    @RequestMapping(value = "main")
    public String main(ModelMap map){
        map.put("loginAdmin",getLoginAdmin());
        return "admin/main";
    }

    @RequestMapping(value = "mainLeft")
    public String mainLeft(ModelMap map,HttpServletRequest request){
        List<SysTreeModel> sysTrees = sysTreeService.findByPrentId(NumberUtils.createLong("1"));
        map.put("sysTrees",sysTrees);
        return "admin/mainLeft";
    }

    protected String getPrefix() {
        return "";
    }

    @RequestMapping(value = "loadTree")
    @ResponseBody
    public Object loadTree(Long id, Long sysTreeId){
        List<Node> result = new ArrayList<Node>();
        long loadId = 0;
        if (sysTreeId != null && id == null) {
            loadId = sysTreeId;
        }else{
            loadId = id;
        }
        List<SysTreeModel> sysTrees = sysTreeService.findByPrentId(loadId);
        Node node=null;
        for (SysTreeModel sysTree : sysTrees) {
            node = new Node();
            node.setId(sysTree.getTreeId());
            node.setName(sysTree.getTreeName());
            node.setText(sysTree.getTreeName());
            node.setUrl(sysTree.getUrl());
            result.add(node);
        }
        return  result;
    }
}
